<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<th:block th:include="include :: header('修改合同付款')" />
<th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-contractPayment-edit"
			th:object="${crmContractPayment}">
			<input name="paymentId" th:field="*{paymentId}" type="hidden">
			<input name="contractId" type="hidden" id="contractId" th:field="*{contractId}" />
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">合同名称：</label>
				<div class="col-sm-8">
					<div class="input-group">   
                    	<input name="contractName" onclick="selectContract()" th:field="*{contract.contractName}" class="form-control"
						type="text" placeholder="请选择所属合同 " id="contractName" required readonly>		
						<span class="input-group-addon"><i class="fa fa-search"></i></span>
					</div>	
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">本次付款金额：</label>
				<div class="col-sm-8">
					<input name="payMoney" th:field="*{payMoney}" class="form-control" type="number" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">付款日期：</label>
				<div class="col-sm-8">
					<div class="input-group date">
						<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
						<input name="payDate"
							th:value="${#dates.format(crmContractPayment.payDate, 'yyyy-MM-dd')}"
							class="form-control" placeholder="yyyy-MM-dd" type="text"
							required>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">付款方式：</label>
				<div class="col-sm-8">
					<div class="radio-box"
						th:each="dict : ${@dict.getType('pay_type')}">
						<input type="radio" th:id="${'payType_' + dict.dictCode}"
							name="payType" th:value="${dict.dictValue}" th:field="*{payType}"
							required> <label th:for="${'payType_' + dict.dictCode}"
							th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">是否需要发票：</label>
				<div class="col-sm-8">
					<div class="radio-box"
						th:each="dict : ${@dict.getType('is_invoice')}">
						<input type="radio" th:id="${'isInvoice_' + dict.dictCode}"
							name="isInvoice" th:value="${dict.dictValue}"
							th:field="*{isInvoice}" required> <label
							th:for="${'isInvoice_' + dict.dictCode}"
							th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
		</form>
	</div>
	<div class="row">
		<div class="col-sm-offset-5 col-sm-10">
			<button type="button" class="btn btn-sm btn-primary" onclick="submit()" shiro:hasPermission="crm:contractPayment:submit">
				<i class="fa fa-location-arrow"></i>提 交
			</button>
			&nbsp;
			<button type="button" class="btn btn-sm btn-success" onclick="submitHandler()">
				<i class="fa fa-check"></i>保 存
			</button>
			&nbsp;
			<button type="button" class="btn btn-sm btn-danger" onclick="closeItem()">
				<i class="fa fa-reply-all"></i>关 闭
			</button>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: datetimepicker-js" />
	<script type="text/javascript">
		var prefix = ctx + "crm/contractPayment";
		var prefixContract = ctx + "crm/contract";
		$("#form-contractPayment-edit").validate({
			focusCleanup : true
		});

		//提交
		function submit() {
			if ($.validate.form()) {
				$.modal.confirm("确认提交吗？", function() {
					$.ajax({
						type : "get",
						contentType : "json",
						url : prefixContract + "/checkPaymentMoney",
						data : {
							"contractId" : $("#contractId").val(),
							"payMoney" : $("#payMoney").val()
						},
						success : function(response) {
							if (!response) {
								$.modal.alertError("付款金额不能大于欠款金额！");
								return;
							} else {
								$.operate.saveTab(prefix + "/submit", $('#form-contractPayment-edit').serialize());
							}
						},
						error : function(e) {
						}
					});
			});
			}
		}
		
		//保存
		function submitHandler() {
			if ($.validate.form()) {
				$.ajax({
					type : "get",
					contentType : "json",
					url : prefixContract + "/checkPaymentMoney",
					data : {
						"contractId" : $("#contractId").val(),
						"payMoney" : $("#payMoney").val()
					},
					success : function(response) {
						if (!response) {
							$.modal.alertError("付款金额不能大于欠款金额！");
							return;
						} else {
							$.operate.saveTab(prefix + "/edit", $('#form-contractPayment-edit').serialize());
						}
					},
					error : function(e) {
					}
				});
			}
		}

		$("input[name='payDate']").datetimepicker({
			format : "yyyy-mm-dd",
			minView : "month",
			autoclose : true
		});

		/*  合同付款管理-新增-选择合同 */
		function selectContract() {
			var url = ctx + "crm/contract/selectContractPayment";
			$.modal.open("选择合同", url);
		}
	</script>
</body>
</html>